﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.BLL.Base;
using iSun.Model;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace iSun.Web.UI.Controls
{
    public class DeptTree : Literal
    {
        private string _tableName = "";
        [Description("数据来源的表名")]
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }

        private string _caption = "";
        [Description("文本")]
        public string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }
        private string _key = "";
        [Description("主键")]
        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }
        private string _parentKey = "";
        [Description("父节点主键")]
        public string ParentKey
        {
            get { return _parentKey; }
            set { _parentKey = value; }
        }
        private string _path = "";
        [Description("节点路径")]
         public string path
        {
            get { return _path; }
            set { _path = value; }
        }
        private bool _isAllowCheck = false;
        [Description("是否允许多选")]
        public bool AllowCheck
        {
            get { return _isAllowCheck; }
            set { _isAllowCheck = value; }
        }
        private int _IsOutDepartment = 0;
        [Description("组织架构类型")]
        public int IsOutDepartment
        {
            get { return _IsOutDepartment; }
            set { _IsOutDepartment = value; }
        }
        private Guid _userID = Guid.Empty;
        [Description("外部人员ID")]
        public Guid userID
        {
            get { return _userID; }
            set { _userID = value; }
        }
        private int _IsVirtual=0;
        [Description("是否虚拟部门")]
        public int IsVirtual
        {
            get { return _IsVirtual; }
            set { _IsVirtual = value; }
        }
        private string _Condition = "";
        [Description("自定义查询条件")]
        public string Condition
        {
            get { return _Condition; }
            set { _Condition = value; }
        }


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            string sql = "";
            IList<TreeViewData> list=null;
            StringBuilder sbChilds=new StringBuilder();
            if (!DesignMode )
            {
            
                StringBuilder _strHtml = new StringBuilder();

                if (AllowCheck)
                    _strHtml.Append("<div  id='"+this.ID+"' class='whitebg ygtv-checkbox'>");
                else
                    _strHtml.Append("<div  id='" + this.ID + "' class='whitebg'>");
                _strHtml.Append("<ul>");
                //得到一级菜单
                //IList<TreeViewData> rootlist = list.Where(p => p.Path.Split('-').Length == 2).OrderBy(p => p.Path).ToList();
                IList<TreeViewData> rootlist = null;
                //判断是否是外部机构的用户
                    //sql="select * from datdepartment where deptid in (select deptid from TrCustomer where  TrCustomerID='"+userID+"')";
                sql = "select DeptID as [key],DeptName as Caption,ParentID as  ParentKey,DeptPath as [Path],DeptType from DatDepartment where (IsVirtualDepartment<>1 or IsVirtualDepartment is null)";
                list = new EntityBLL().GetEntitysBySqlComm<TreeViewData>(sql);
                rootlist = list.Where(p => p.Path=="@001").OrderBy(p => p.Path).ToList();
                int i = 0;
                if (rootlist != null)
                {
                    foreach (TreeViewData tree in rootlist)
                    {

                        _strHtml.Append("<li>");
                        if (!AllowCheck)
                        {
                            _strHtml.Append("<span><a href=\"#" + tree.Key + "\" style='display:block;cursor:hand' onclick=\"Native('" + tree.Caption + "','" + tree.Key + "','" + tree.DeptType + "')\">" + tree.Caption + "</a></span>");
                        }
                        else
                        {
                            _strHtml.Append("<a href='#@" + tree.Key + "'>" + tree.Caption + "</a>");
                        }

                        //功能明细
                        AddNode(list, tree.Key, _strHtml);
                        _strHtml.Append("</li>");
                        i++;
                    }
                }
                else
                {
                    _strHtml.Append("<li>权限不足或数据不存在</li>");
                }
                _strHtml.Append("</ul>");
                _strHtml.Append("</div>");
                writer.Write(_strHtml.ToString());
                base.Render(writer);
            }
            else
            {
                writer.Write("树型菜单");
            }
        }
        private void AddNode(IList<TreeViewData> list, Guid? key,StringBuilder sb)
        {
            var detail = list.Where(p => p.ParentKey==key).OrderBy(p => p.Path).ToList();
            if (detail.ToList().Count > 0)
            {
                sb.Append("<ul>");
                foreach (TreeViewData v in detail)
                {
                    sb.Append("<li>");
                    if (!AllowCheck)
                    {
                        sb.Append("<span><a href=\"#" + v.Key + "\" style='display:block;cursor:hand' onclick=\"Native('" + v.Caption + "','" + v.Key + "','" + v.DeptType + "')\">" + v.Caption + "</a></span>");
                    }
                    else
                    {
                        sb.Append("<a href='#@" + v.Key + "'>" + v.Caption + "</a>");
                    }
                    AddNode(list, v.Key, sb);
                    sb.Append("</li>");

                }
                sb.Append("</ul>");
            }
            //childName += "</ul>";
          
         
        }

   
    }
}
